﻿using System;
using System.Collections.Generic;
using KnowledgeSource;
using NHunspell;

namespace BlackBoard
{
    public class Controller
    {
        public List<string> knowledgeSources = new List<string>();
        public List<Entry> entries = new List<Entry>();
        private MyThes thes; 
        private Hunspell hunspell; 
        public int pointer = 1;

        public Controller(List<string> sources)
        {
            knowledgeSources = sources;
        }

        public void Loop(string input)
        {
            entries = new List<Entry>();
            Entry entry = new Entry();
            var temp = new List<string>();
            temp.Add(input);
            foreach (var knowledgeSource in knowledgeSources)
            {  
                entry = new Entry();
                entry.Result = Execute(knowledgeSource, temp);
                entry.Time = DateTime.Now;
                entry.Turn = pointer;
                entry.KnowledgeSourceName = knowledgeSource;
                entries.Add(entry);
                temp = entry.Result;
            }

            pointer++;
        }

        public void FirstRunLoop(string input)
        {
            entries = new List<Entry>();
            Entry entry = new Entry();
            var temp = new List<string>();
            temp.Add(input);
            foreach (var knowledgeSource in knowledgeSources)
            {
                entry = new Entry();
                entry.Result = FirstRun(knowledgeSource, temp);
                entry.Time = DateTime.Now;
                entry.Turn = pointer;
                entry.KnowledgeSourceName = knowledgeSource;
                entries.Add(entry);
                temp = entry.Result;
            }

            pointer++;
        }
        public List<string> FirstRun(string knowledgeSourceName, List<string> input)
        {
            if (knowledgeSourceName == "Normalize")
            {
                var result = new List<string>();
                var temp = new Normalize();
                result.Add(temp.execute(input[0]));
                return result;
            }
            if (knowledgeSourceName == "RemoveForbiddenCharacters")
            {
                var result = new List<string>();
                var temp = new RemoveForbiddenCharacters();
                result.Add(temp.execute(input[0]));
                return result;
            }
            if (knowledgeSourceName == "FindingSynonyms")
            {
                return input;
            }
            if (knowledgeSourceName == "PolishCharactersRemove")
            {
                var result = new List<string>();
                var temp = new PolishCharacterRemove();
                result.Add(temp.execute(input[0]));
                return result;
            }
            throw new ArgumentException("Knowledge source does not exist");
        }

        public List<string> Execute(string knowledgeSourceName, List<string> input)
        {
            if( knowledgeSourceName == "Normalize")
            {
                var result = new List<string>();
                var temp = new Normalize();
                foreach (var i in input)
                {
                    result.Add(temp.execute(i));
                }
                return result;
            }
            if (knowledgeSourceName == "RemoveForbiddenCharacters")
            {
                var result = new List<string>();
                var temp = new RemoveForbiddenCharacters();
                foreach (var i in input)
                {
                    result.Add(temp.execute(i));
                }
                return result;
            }
            if (knowledgeSourceName == "FindingSynonyms")
            {
                string appDomain = AppDomain.CurrentDomain.BaseDirectory;
                thes = new MyThes(appDomain + @"..\..\source\KnowledgeSource\KnowledgeSource\KnowledgeSource\DictionariesForNHunspell\th_pl_PL_v2.dat");
                hunspell = new Hunspell();
                hunspell.Load(appDomain + @"..\..\source\KnowledgeSource\KnowledgeSource\KnowledgeSource\DictionariesForNHunspell\pl_PL.aff",
                              appDomain + @"..\..\source\KnowledgeSource\KnowledgeSource\KnowledgeSource\DictionariesForNHunspell\pl_PL.dic");

                var sc = new SpellChecker(hunspell);
                var vti = new VerbToInfinitive(hunspell, thes);
                var fs = new FindingSynonyms(hunspell, thes);
                var output = new List<String>();
                output.Add(input[0]);
                foreach (var i in input)
                {
                    var temp_sc = sc.execute(input[0]);
                    foreach (var input_vti in temp_sc)
                    {
                        var temp_vti = vti.execute(input_vti);
                        foreach (var input_fs in temp_vti)
                        {
                            var temp_fs = fs.execute(input_fs);
                            output.AddRange(temp_fs);
                        }
                    }
                    output.AddRange(output);
                }


                return output;
            }
            if (knowledgeSourceName == "PolishCharactersRemove")
            {
                var result = new List<string>();
                var temp = new PolishCharacterRemove();
                foreach (var i in input)
                {
                    result.Add(temp.execute(i));
                }
                return result;
            }
            throw new ArgumentException("Knowledge source does not exist");

        }
    }
}
